<?php
namespace app\m\controller;
use x\Container;
class Goods extends Base{

    public function lists()
    {

        $d = $this->get();
        $order = 0;
        $map = [];
        $page = 1;
        if(isset($d["order"]))$order=$d["order"];
        if (isset($d["cat_id"]))
        {
            $ids = m("GoodsCat", "c")->getAllchildCat($d["cat_id"]);
            if($ids){
                $str = "";
                foreach($ids as $v){
                    $str .= "?,";
                }
                $str = rtrim($str,",");
                $map[] = ["and", "cat_id in (".$str.")", $ids];
            }else{
                $map[] = ["and", "cat_id", "=", $d["cat_id"]];

            }
        }
        if(isset($d["keyword"]))$map[] =["and","title","like",'%' . $d["keyword"] . '%'];
        if(isset($d['page']))$page = $d["page"];
        $map[] = ["and", "status", "=", 0];
        $goods_list = m("goods","c")->lists($map,$order, $page);
        $list = [
            "goods_list"=>$goods_list[0],
            "page_count"=>$goods_list[1],
            "page" => $page,
            'keyword' => isset($d['keyword']) ? $d['keyword'] : ''
        ];
        if(is_post()){
            $html = "";
            foreach ($goods_list[0] as $k => $v) {
                $html .="<div class='goods'><a href='".url('goods/detail',['id'=>$v['id']])."'>";
                $html .= "<img src='{$v['pic']}'>";
                $html .= "<p class='name myc'>{$v['title']}</p>";
                $html .= "<p class='price key'>￥&nbsp;{$v['price']}</p>";
                $html .= "</a></div>";
            }
            suc($html);
        }
        return $this->fetch($list);
    }

    function detail(){
        $id = $this->get('id');
        if($id){
            $rs = $this->db->query('select id from goods_price where goods_id=? order by id asc',[$id],1);
            r(url('goods/detail')."?gid=".$rs['id']);
        }
        $gid = $this->get('gid');
        cookie("footpoint_" . $id, $id);
        $info = m("goods", "c")->detail($gid);
        $info['spec'] = htmlspecialchars_decode($info['spec']);
        $info['content'] = htmlspecialchars_decode($info['content']);
        if($info['pics']){
          $info['pics'] = explode(",",$info["pics"]);
        }
        //同分类下人气商品
        $map = [
            ["and", "id", "!=", $info["goods_id"]]
        ];
        $cat_goods = m("goods", "c")->lists($map, 3, 1, 2);
        //人气累加
        $d = [
            "views" => $info['p_views'] + 1,
            "id" => $id
        ];
        $this->db->update("goods_price", $d);
        $info['spec'] = htmlspecialchars_decode($info['spec']);
        //属性
        $attr = [];
        $spec = json_decode($info['spec'],true);
        $ptitle = explode("_",$info['p_title']);
        $temp =[];
        $i = 0;
        foreach($spec as $k => $v){
            $temp[$k] = [];
            foreach($v as $key => $val){
                $checked = $val==$ptitle[$i] ? true:false;
                $arr = [
                    $val,
                    $checked,
                ];
                $temp[$k][] = $arr;
            }
            $i++;
        }
        $iscollect = 0;
        if($this->userinfo){
            $collectinfo = $this->db->query('select * from goods_collect where goods_id=? and user_id=?',[$info['goods_id'],$this->userinfo["id"]]);
            if($collectinfo){
                $iscollect = 1;
            }
        }
        $d = [
            "iscollect" => $iscollect,
            "goods_info" => $info,
            "cat_goods" => $cat_goods[0],
            'spec' => $temp,
        ];
        return $this->fetch($d,'',false);

    }

    //ajax获取商品ID
    public function findGoods(){
        $d = $this->post;
        $info = $this->db->query('select * from goods_price where title = ? and goods_id =?',[substr($d['title'],1),$d['goods_id']],1);
        cookie('js_url', url('goods/detail')."?gid=".$info['id']);
        suc();
    }

    function cat(){
        $cat_list = $this->db->cache("select * from goods_cat where status=0 order by sort desc,id desc");
        if(is_post()){
            $pid = $this->post('pid');
        }
        $new_list = [];
        foreach ($cat_list as $k => $v) {
            if($v['parent_id'] != 0){
                continue;
            }
            if(isset($pid) && $v['id'] != $pid && $pid != 0){
                continue;
            }
            $child_cat = [];
            foreach ($cat_list as $ke => $va) {
                if($v["id"] == $va['parent_id']){
                    foreach ($cat_list as $key => $val) {
                        if($va["id"] == $val['parent_id']){
                            $va["child_list"][] = $val;
                        }
                    }
                    $child_cat[] = $va;
                }
            }
            $v["child_list"] = $child_cat;
            $new_list[] = $v;

        }
        if(is_post()){
            suc(['list'=>$new_list]);
        }
        return $this->fetch(['list'=>$new_list]);
    }

    public function getCategory(){
      $category = $this->com["categorys"];
      echo json_encode($category);
    }

    /**
     * 添加购物车
     */
    public function addCart(){
        $this->checkLogin();
        $d = $this->post();
        $rule = [
            'attr_id'=>'require|integer',
            'nums'=>'require|integer',
        ];
        $msg = [];
        check($rule,$msg,$d);
        $cart_count = m("goodsCart", "c")->add($d['attr_id'],$d['nums'], $this->userinfo['id']);
        if($cart_count){
            cookie('js_url', url('/user/myCart'));
            suc(["msg" => "添加购物车成功", "count" => $cart_count]);
        }else {
            err(m("goodsCart", "c")->getError());
        }
    }
}
?>
